package com.lzh.edu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzh.commonutils.CommonResult;
import com.lzh.commonutils.CourseInfo;
import com.lzh.commonutils.JwtUtils;
import com.lzh.edu.client.OrderClient;
import com.lzh.edu.entity.Course;
import com.lzh.edu.query.CourseQuery;
import com.lzh.edu.query.TeacherQuery;
import com.lzh.edu.service.ChapterService;
import com.lzh.edu.service.CourseService;
import com.lzh.edu.vo.CourseInfoVo;
import com.lzh.edu.vo.course.ChapterVo;
import com.lzh.edu.vo.course.CourseFrontVo;
import com.lzh.edu.vo.course.CoursePublishVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lizuhu
 * @since 2021-04-24
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/course")
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "根据条件分页查询")
    @GetMapping("/pageList")
    public CommonResult pageList(@RequestParam Long page,
                                 @RequestParam Long rows,
                                 CourseQuery courseQuery) {
        Page<Course> coursePage = new Page<>(page,rows);
        courseService.pageQuery(coursePage, courseQuery);
        //每一页的数据
        List<Course> records = coursePage.getRecords();
        //总记录数
        long total = coursePage.getTotal();
        return CommonResult.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "无条件分页查询")
    @GetMapping("/getPage")
    public CommonResult pageList1(@RequestParam Long page,@RequestParam Long rows) {
        Page<Course> pageParams = new Page<>(page,rows);
        courseService.pageList(pageParams);
        List<Course> records = pageParams.getRecords();
        long total = pageParams.getTotal();
        return CommonResult.ok().data("total",total).data("rows",records);
    }


    @ApiOperation(value = "查询所有课程")
    @GetMapping("/getCourseList")
    public CommonResult getCourseList() {
        List<Course> courseList = courseService.list(null);
        return CommonResult.ok().data("items",courseList);
    }

    @ApiOperation(value = "添加课程信息")
    @PostMapping("/addCourseInfo")
    public CommonResult addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //将课程id返回
        String id = courseService.saveCourseInfo(courseInfoVo);
        return CommonResult.ok().data("courseId", id);
    }

    @ApiOperation(value = "根据课程id查询课程")
    @GetMapping("/getCourseInfo")
    public CommonResult getCourseInfo(@RequestParam String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return CommonResult.ok().data("items",courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PutMapping("/updateCourse")
    public CommonResult updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return CommonResult.ok();
    }

    @ApiOperation(value = "获取发布课程基本信息")
    @GetMapping("/getPublishCourse")
    public CommonResult getPublishCourseInfo(@RequestParam String courseId) {
        CoursePublishVo publishCourse = courseService.getPublishCourse(courseId);
        return CommonResult.ok().data("publishCourse",publishCourse);
    }

    @ApiOperation(value = "发布课程")
    @PutMapping("/publishCourse")
    public CommonResult publishCourse(@RequestParam String courseId) {
        courseService.publishCourse(courseId);
        return CommonResult.ok();
    }

    /**
     * 根据课程id删除课程以及云端视频
     * @param id
     * @return
     */
    @ApiOperation(value = "根据课程id删除课程")
    @DeleteMapping("/deleteCourse")
    public CommonResult removeCourseById(@RequestParam String id) {
       Boolean result = courseService.removeCourseById(id);
       if (result) return CommonResult.ok();
       else return CommonResult.error().message("删除课程失败");
    }

    @ApiOperation(value = "前端获取课程信息")
    @GetMapping("/getWebCourse")
    public CommonResult getById(@RequestParam String id, HttpServletRequest request) {
        //查询课程信息和讲师信息
        CourseFrontVo course = courseService.selectInfoWebById(id);
        //查询当前课程的章节信息
        List<ChapterVo> chapter = chapterService.getChapterByCourseId(id);

        //调用远程服务查询订单是否被支付 true已支付 false未支付
        //第一个参数：根据token获取用户id
        //第二个参数：课程id
        boolean buyCourse = orderClient.isBuyCourse(JwtUtils.getUserIdByToken(request), id);
        return CommonResult.ok().data("course",course).data("chapter",chapter).data("isPay",buyCourse);
    }

    @ApiOperation(value = "订单页查询课程信息")
    @GetMapping("/getCourseByOrder")
    public CourseInfo getCourseByOrder(@RequestParam String courseId) {
        CourseInfo courseInfo = courseService.getCourseByOrder(courseId);
        return courseInfo;
    }

}

